package com.kbbywyclient.presenter;

import android.content.Context;
import android.text.TextUtils;

import com.kbbywyclient.data.entity.MinePartnerData;
import com.kbbywyclient.data.model.MinePartnerDataModel;
import com.kbbywyclient.presenter.base.BasePresenter;
import com.kbbywyclient.service.ApiService;
import com.kbbywyclient.service.ApiServiceHelper;
import com.kbbywyclient.ui.contarct.MinePartnerContract;
import com.kbbywyclient.util.HttpExcUtil;
import com.kbbywyclient.util.RsaJsonUtil;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import rx.Subscriber;

/**
 * Created by Jiangzx on 2017/11/2 11:42.
 */

public class MinePartnerPresenter extends BasePresenter implements MinePartnerContract.Presenter {
    public static final int DEFAULT_PAGE=0;
    @Inject
    Context context;
    @Inject
    ApiService apiService;
    @Inject
    RsaJsonUtil jsonUtil;

    private MinePartnerContract.View view;

    public int page=DEFAULT_PAGE;

    private List<MinePartnerData.Content.Partner> partners=new ArrayList<>();

    @Inject
    public MinePartnerPresenter(MinePartnerContract.View view){
        this.view=view;
    }

    @Override
    public void loadHeader(int userId) {
        jsonUtil.put("saleID",userId);
        ApiServiceHelper.
                appointThreadNormal(apiService.getPartnerHeader(jsonUtil.getRsaParams())).
                subscribe(new Subscriber<MinePartnerDataModel.Header>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        HttpExcUtil.printError(e);
                    }

                    @Override
                    public void onNext(MinePartnerDataModel.Header header) {
                        MinePartnerData.Header data=header.getData();
                        if(data!=null){
                            List<MinePartnerData.Header.Role> roles=
                                    new ArrayList<>();
                            MinePartnerData.Header.Role role=
                                    new MinePartnerData.Header.Role();
                            role.setRoleName("级别");
                            role.setRole(0);
                            roles.add(role);
                            List<MinePartnerData.Header.Role> tempRoles=
                                    data.getRoles();
                            if(tempRoles!=null&&tempRoles.size()>0){
                                roles.addAll(tempRoles);
                            }

                            List<MinePartnerData.Header.City> cities=
                                    new ArrayList<>();
                            MinePartnerData.Header.City city=
                                    new MinePartnerData.Header.City();
                            city.setCityId("0");
                            city.setCityName("城市");
                            cities.add(city);
                            List<MinePartnerData.Header.City> tempCities=data.getCitys();
                            if(tempCities!=null&&tempCities.size()>0){
                                cities.addAll(tempCities);
                            }

                            data.setRoles(roles);
                            data.setCitys(cities);
                            view.onHeaderSuccess(data);
                        }
                    }
                });
    }

    @Override
    public void loadList(int userId, String keyWord, int city, int role, int page) {
        jsonUtil.put("saleID",userId);
        if(!TextUtils.isEmpty(keyWord)){
            jsonUtil.put("keyWord",keyWord);
        }
        if(city!=0){
            jsonUtil.put("city",city);
        }
        if(role!=0){
            jsonUtil.put("role",role);
        }
        jsonUtil.put("pageNum",page);
        ApiServiceHelper.
                appointThreadNormal(apiService.getPartnerContent(jsonUtil.getRsaParams())).
                subscribe(new Subscriber<MinePartnerDataModel.Content>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        HttpExcUtil.printError(e);
                        MinePartnerPresenter.this.page--;
                        if(MinePartnerPresenter.this.page<DEFAULT_PAGE){
                            MinePartnerPresenter.this.page=DEFAULT_PAGE;
                        }
                    }

                    @Override
                    public void onNext(MinePartnerDataModel.Content content) {
                        MinePartnerData.Content contentData=content.getData();
                        if(contentData!=null){
                            List<MinePartnerData.Content.Partner> temp=
                                    contentData.getPartner();
                            if(temp!=null&&temp.size()>0){
                                partners.addAll(temp);
                            }
                            view.completeLoadContent((page+1)<contentData.getTotalPage(),
                                    partners);
                        }
                    }
                });
    }

    @Override
    public void clearListData() {
        page=DEFAULT_PAGE;
        partners.clear();
    }
}
